<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
@include('public.h5_head')
    <title>购买助手</title>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/app.js"></script>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/integral_tool.css"/>
    <script src="{{ env('CDN_DOMAIN_NAME') }}/js/commonjs/rem.js"></script>
    <link rel="stylesheet" type="text/css" href="{{ env('CDN_DOMAIN_NAME') }}/css/commoncss/common.css"/>
</head>
<body>
<div id="box_simulate">
    <input type="hidden" value="{{ $userinfo->integral }}" class="init_score">
    <div class="wrap">
        <div class="simulate">
            <ul class="simulate_ul">
                <li class="simulate_li">
                	<select name="" class="simulate_select" onchange="grade(this)">
						<option name="0" value="0">LV0</option>
						<option name="1" value="100000">LV1</option>
						<option name="2" value="200000">LV2</option>
						<option name="3" value="400000">LV3</option>
						<option name="4" value="800000">LV4</option>
						<option name="5" value="1600000">LV5</option>
						<option name="6" value="3200000">LV6</option>
						<option name="7" value="6400000">LV7</option>
						<option name="8" value="12800000">LV8</option>
						<option name="9" value="25600000">LV9</option>
					</select>
                    <span class="simulate_sp">历史积分:</span>
                    <p class="simulate_num1">{{ $userinfo->integral }}</p>
                </li>
                <li class="simulate_li">
					<span class="simulate_sp">当年消费:</span>
					<p class="simulate_p simulate_m" style="color:red;">￥</p>
					<p class="simulate_num3"><span class="simulate_hide" style="display:none;">{{ $userinfo->actual_integral }}</span><span class="simulate_now">{{ $userinfo->actual_integral }}</span>/<span class="simulate_next">0</span></p>
				</li>
                <li class="simulate_li">
                    <span class="simulate_sp">当年预算:</span>
                    <p class="simulate_p" style="color:red;width:1rem;margin-right:0.4rem;">￥</p>
                    <input type="tel" maxlength="11"  class="simulate_num2 simulate_inp">
                </li>
            </ul>
            <div class="simulate_consume">
                <span>消费方案:</span>
                <button type="button" id="add-programme" class="simulate_add add-programme">继续购买</button>
            </div>
            <ol class="programme-model">
                
            </ol>           
            <div class="simulate_bottom">
            	<div class="simulate_t">
					<span style="float:left;">预算结余:</span>
					<span style="color:red;float:left;margin-left:0.5rem;">￥</span>
					<div class="surplus_price"></div>

				</div>
                @if($userinfo->is_bus ==1)
                    <div class="simulate_b">
                        <span style="float:left;">年底返款:</span>
                        <span style="color:red;float:left;margin-left:0.5rem;">￥</span>
                        <div class="simulate_money"></div>
                    </div>
                @elseif($userinfo->is_bus ==0)

                @endif
				<div class="simulate_t">
					<span style="float:left;">优惠总计:</span>
					<span style="color:red;float:left;margin-left:0.5rem;">￥</span>
					<div class="surplus_total"></div>
                    @if($userinfo->is_bus ==1)
                        <button type="button" class="simulate_btn" disabled>年底清算</button>
                    @elseif($userinfo->is_bus ==0)
                        <button type="button" class="simulate_btn" disabled>优惠总计</button>
                    @endif
				</div>              
            </div>
        </div>
        <meta name="csrf_token" id="token" content="{{ csrf_token() }}">
    </div>
</div>
<div class="simulate_hint" style="display:none;"></div>
</body>
<script src="/static/js/commonjs/iscroll.js"></script>
<script>
    //初始历史积分判断等级
    $(document).ready(function () {
        var user_vip_level = history_score($('.init_score').val());
        var all_score = now_score($('.simulate_hide').text());
        $('.simulate_next').text(all_score);
        $(".simulate_select option[name='"+user_vip_level+"']").prop("selected",true);
    });
    $(document).on('click','.add-programme',function(){
        var budget_price = $('.simulate_num2').val();
        if(budget_price.length <= 0)
        {
            $(".simulate_hint").html("请先输入预算总消费")
            $(".simulate_hint").show();
            setTimeout(function(){
                $(".simulate_hint").hide();
            },800)
            return false
        }

        var num = $('.programme_num').length+1;
        var model="<li class='programme_num'><div class='programme_num_top'><span class='programme_sp'>第<i>"+num+"</i>次消费:</span><p class='programme_p' style='color:red;'>￥</p><input type='tel' class='programme'><button type='button' class='del-programme'>删除</button></div><div class='programme_num_bottom'></div></li>"
        setTimeout(function () {
            myscroll.refresh();
        }, 10);
        $('.programme-model').append(model);
    });


    $(document).on('click','.del-programme',function () {
        $(this).parent().parent('.programme_num').remove();
        for(var i =0 ; i < $('.programme_num').length ; i++)
        {
            $('.programme_num').eq(i).children().children().children('i').text(i+1);
        }
        var count=0;
        $('.programme').each(function () {
            if($('.programme').length > 0 )
            {
            	var _score = $(this).val();
                if(_score.length <= 0){
                    _score = 0;
                }
                count += parseInt($(this).val());
            }
        });
        $(".simulate_num1").html(parseInt($(".init_score").val())+count);//历史积分变化
        $(".simulate_now").html( count+ parseInt($(".simulate_hide").html()));
        var vip_integral = now_score($('.simulate_now').html());           	
        $(".simulate_next").html(vip_integral);
        if(count>parseInt($(".simulate_num2").val())){
        	$(".simulate_num2").css({"border":"1px solid red"});
    		$(".simulate_hint").html("消费金额超额")
            $(".simulate_hint").show();
            setTimeout(function(){
                $(".simulate_hint").hide();
            },800)
    	}else{
    		$(".simulate_num2").css({"border":"1px solid #ccc"});
    	}
    	level();
    	
    	var total_price = $('.simulate_num2').val();
        var actual_price = total_price-count;
        if(actual_price >= 0 )
        {
            $('.surplus_price').html(total_price-count);
            $(".simulate_btn").prop('disabled',true);
            $(".simulate_btn").css({"background":"#ccc"});
            if(actual_price==0){
            	$(".simulate_btn").prop('disabled',false);
            	$(".simulate_btn").css({"background":"#fff"});
            }
        }else{
            $('.surplus_price').html('预算消费不足');
        }
    	
    	
    	
    	
    	
    })
    var myscroll;
    function loaded(){
        setTimeout(function(){
            myscroll=new IScroll(".wrap",{
                click:true,

            })
        },100);
    }
    window.addEventListener("load",loaded,false);
    //选择等级，出现最小历史积分
    function grade(sel) {  
    	$('.init_score').val(sel.options[sel.selectedIndex].value)
        $(".simulate_num1").html(sel.options[sel.selectedIndex].value)
    }
	//历史积分判断等级
    $('.simulate_num1').bind('input propertychange', function() {
        var user_vip_level = history_score($(this).val());
        //$('.user_lv').text(user_vip_level);
        $(".simulate_select option[name='"+user_vip_level+"']").prop("selected",true);
    });


    function score_str() {
        var score = '';        
        $('.programme').each(function () {
			
            if($('.programme').length > 0 )
            {
                if($(this).val() > 0)
                {
                    score += $(this).val()+",";
                }else{
                    score += "0,";
                }
            }
        });
        return score;
    }
    //年底清算
    $('.simulate_btn').click(function () {
        var history_score = $('.init_score').val();
        var score = score_str();
        if(score.length <= 0)
        {
        	$(".simulate_hint").html("输入消费金额")
        	$(".simulate_hint").show();
        	setTimeout(function(){
        		$(".simulate_hint").hide();
        	},800)
           // alert(' 输入消费金额');
            return false;
        }
        $.ajax({
            url:'/public/year_score_cal',
            type:'POST',
            data:{score:score,history_score:history_score},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
            },
            dataType:'json',
            success:function(data){
                console.log(data)
                if(data.success == true){
                	var total_price =Number($('.simulate_num2').val());
                	var n=total_price-data.msg;
                    console.log(n)
                    var n= n.toFixed(2);
                    console.log(n)
					$(".simulate_money").html(""+data.info+"");
					$(".surplus_total").html(""+n+"");
                }else{

                }
            }
        })
    });
    // 预算总消费不能为数字
	$('.simulate_num2').on('input propertychange',function () {
		if(isNaN($(this).val())){
        	$(".simulate_hint").html("请输入正确的价格")
        	$(".simulate_hint").show();
        	setTimeout(function(){
        		$(".simulate_hint").hide();
        	},800)
        }
        var total_price = $('.simulate_num2').val();
        var count=0;
        $('.programme').each(function () {
            if($('.programme').length > 0 )
            {
                var _score = $(this).val();
                if(_score.length <= 0){
                    _score = 0;
                }
                count += parseInt(_score);
            }
        });
        var actual_price = total_price-count;
        if(actual_price >= 0 )
        {
            $('.surplus_price').html(total_price-count);
            $(".simulate_btn").prop('disabled',true);
            $(".simulate_btn").css({"background":"#ccc"});
            if(actual_price==0){
            	$(".simulate_btn").prop('disabled',false);
            	$(".simulate_btn").css({"background":"#fff"});
            }
        }else{
            $('.surplus_price').html('预算消费不足');
        }
	})



    // 消费方案 实时监控
    $(document).on('input propertychange','.programme',function () {
        var history_score = $('.init_score').val();
        var total_price = $('.simulate_num2').val();
        var score = score_str();

//        $('.surplus_price').html(total_price-score);
        //不是数字的时候
        if(isNaN($(this).val())){
        	$(".simulate_hint").html("请输入正确的消费金额")
        	$(".simulate_hint").show();
        	setTimeout(function(){
        		$(".simulate_hint").hide();
        	},800)
        }
        if(score.length <= 0){
        	$(".simulate_hint").html("输入消费金额")
        	$(".simulate_hint").show();
        	setTimeout(function(){
        		$(".simulate_hint").hide();
        	},800)
            //alert(' 输入消费金额');
            return false;
        }else{
        	//金额大于预算
        	var count=0;
            $('.programme').each(function () {
                if($('.programme').length > 0 )
                {
                	var _score = $(this).val();
                	if(_score.length <= 0){
                		_score = 0;
                	}
                    count += parseInt(_score);
                }
            });
            var actual_price = total_price-count;
            if(actual_price >= 0 )
	        {
	            $('.surplus_price').html(total_price-count);
	            $(".simulate_btn").prop('disabled',true);
	            $(".simulate_btn").css({"background":"#ccc"});
	            if(actual_price==0){
	            	$(".simulate_btn").prop('disabled',false);
	            	$(".simulate_btn").css({"background":"#fff"});
	            }
	        }else{
	            $('.surplus_price').html('预算消费不足');
	        }

            $(".simulate_num1").html(parseInt($(".init_score").val())+count);//历史积分变化
            $(".simulate_now").html( count+ parseInt($(".simulate_hide").html()));
            var vip_integral = now_score($('.simulate_now').html());        	
        	$(".simulate_next").html(vip_integral);
        	if(count > ( parseInt( $(".simulate_num2").val() ))){
        		$(".simulate_num2").css({"border":"1px solid red"});
                $(".simulate_hint").html("消费金额超额")
                $(".simulate_hint").show();
                setTimeout(function(){
                    $(".simulate_hint").hide();
                },800)
        	}else{
        		$(".simulate_num2").css({"border":"1px solid #ccc"});
        	}
        }
        $.ajax({
            url:'/public/getIntegralInfos',
            type:'POST',
            data:{score:score,history_score:history_score},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
            },
            dataType:'json',
            success:function(data){
                console.log(data)
                if(data.success == true){
					var arr=data.info;
                	if(arr.length>0){
                		for(var i=0; i<arr.length;i++){
                			if(arr[i].actual_score>0){
	                			$('.programme_num').eq(i).children('.programme_num_bottom').html("实付金额:"+arr[i].actual_score+";&nbsp;优惠金额:"+arr[i].discount+"");
                			}else{
                				$('.programme_num').eq(i).children('.programme_num_bottom').html('');
                			}
                		}
                	}
                }else{

                }
            }
        })
        level();
    });
	function level(){
		//console.log(parseInt($(".simulate_num1").html()))
    	var level=history_score(parseInt($(".simulate_num1").html()))
    	$(".simulate_select option[name='"+level+"']").prop("selected",true);
    	$(".simulate_select option[name='"+level+"']").siblings().prop("selected",false);
    }

    function history_score(score) {
        if( score >= 0 && score < 100000)
        {
            return 0;
        }else if (score >= 100000 && score < 200000)
        {
            return 1;
        }else if (score >= 200000 && score < 400000)
        {
            return 2;
        }else if (score >= 400000 && score < 800000)
        {
            return 3;
        }else if (score >= 800000 && score < 1600000)
        {
            return 4;
        }else if (score >= 1600000 && score < 3200000)
        {
            return 5;
        }else if (score >= 3200000 && score < 6400000)
        {
            return 6;
        }else if (score >= 6400000 && score < 12800000)
        {
            return 7;
        }else if (score >= 12800000 && score < 25600000)
        {
            return 8;
        }else if (score >= 25600000 )
        {
            return 9;
        }
        return -1;
    }
	//当年积分
	function now_score(integral){
		if( integral >= 0 && integral < 100000){
            return 100000;
        }else if (integral >= 100000 && integral < 200000){
            return 200000;
        }else if (integral >= 200000 && integral < 300000){
            return 300000;
        }else if (integral >= 300000 && integral < 400000){
            return 400000;
        }else if (integral >= 400000 && integral < 500000){
            return 500000;
        }else if (integral >= 500000 && integral < 600000){
            return 600000;
        }else if (integral >= 600000 && integral < 700000){
            return 700000;
        }else if (integral >= 700000 && integral < 800000){
            return 800000;
        }else if (integral >= 800000 && integral < 900000){
            return 900000;
        }else if (integral >= 900000 && integral < 1000000){
            return 1000000;
        }else if (integral >= 1000000 && integral < 1280000){
            return 12800000;        
        }else if (integral >= 12800000 ){
            return "--";
        }
	}
</script>
</html>
